package com.gack.business.service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gack.business.dao.StoreFacityAndServiceNewDao;
import com.gack.business.model.StoreSupportFacity;
import com.gack.business.model.StoreSupportService;
import com.gack.business.repository.StoreSupportFacityRepository;
import com.gack.business.repository.StoreSupportServiceRepository;
import com.gack.helper.common.abstractobj.Result;

/**
 * 
* @ClassName: StoreSupportFacityAndServiceService 
* @Description: TODO(这里用一句话描述这个类的作用) 
* @author (ZhangXinYu)  
* @date 2018年6月4日 上午11:30:27 
* @version V1.0
 */
@Service
public class StoreSupportFacityAndServiceService implements StoreSupportFacityAndServiceServiceInterface{

	@Autowired
	private StoreSupportFacityRepository facityRepository;
	@Autowired
	private StoreSupportServiceRepository serviceRepository;
	@Autowired
	private BackLogServiceInterface backLogService;
	@Autowired
	private StoreFacityAndServiceNewDao facityAndServiceNewDao;
	
	@Override
	public Result AddStoreSupportFacity(String userid,String facityName) {
		Result result = new Result();
		facityName = facityName.trim();
		List<StoreSupportFacity> facitys = facityRepository.findByName(facityName);
		if(facitys != null && facitys.size() > 0){
			result.setKey("error_duplicateName");
			result.setMsg("配套设施已存在，请勿重复添加");
			return result;
		}
		StoreSupportFacity facity = new StoreSupportFacity();
		facity.setName(facityName);
		facity.setCreatetime(new Date());
		facity.setIsUse(0);
		facity = facityRepository.save(facity);
		backLogService.addBackLog(userid, "增加门店配套设施>> 门店配套设施id："+facity.getId() + " 门店配套设施名称："+facity.getName());
		result.setKey("success");
		result.setValue(facity);
		return result;
	}

	@Override
	public Result AddStoreSupportService(String userid,String serviceName) {
		Result result = new Result();
		serviceName = serviceName.trim();
		List<StoreSupportService> services = serviceRepository.findByName(serviceName);
		if(services != null && services.size() > 0){
			result.setKey("error_duplicateName");
			result.setMsg("配套服务已存在，请勿重复添加");
			return result;
		}
		StoreSupportService service = new StoreSupportService();
		service.setName(serviceName);
		service.setCreatetime(new Date());
		service.setIsUse(0);
		service = serviceRepository.save(service);
		backLogService.addBackLog(userid, "增加门店配套服务>> 门店配套服务id："+service.getId()+" 门店配套服务名称："+service.getName());
		result.setKey("success");
		result.setValue(service);
		return result;
	}

	/**
	 * 		for (String facityName : facities) {
			if(facityRepository.getCountOfSameName(facityName)>0){
				duplicateNames.append(facityName);
				duplicateNames.append(",");
				continue;
			}
			StoreSupportFacity storeSupportFacity = new StoreSupportFacity();
			storeSupportFacity.setName(facityName);
			storeSupportFacity.setCreatetime(new Date());
			storeSupportFacity = facityRepository.save(storeSupportFacity);
			saveFacities.add(storeSupportFacity);
		}
		if (duplicateNames.length() > 0) {
			duplicateNames.substring(0, duplicateNames.length()-1);
			duplicateNames.append(" 配套设施名称重复，重复设施添加失败");
			result.setKey("error");
			result.setMsg(duplicateNames.toString());
			result.setValue(saveFacities);
			return result;
		}
	 */
	@Transactional
	@Override
	public Result addStoreSupportFacities(String userid,String facityNames) {
		Result result = new Result();
		List<String> f = Arrays.asList(facityNames.split("@;-;@"));
		List<String> facities = new ArrayList<>(f);
		//获取被删除的门店配套设施
//		List<StoreSupportFacity> storeSupportFacities = facityRepository.getBeDealStoreSupportFacity(facities);
		//删除不存在的门店配套设施
		facityRepository.dealNotExistsStoreSupportFacity(facities);
		//获取新的门店配套设施
		List<String> allFacityNames = facityRepository.getAllStoreSupportFacityNames();
		facities.removeAll(allFacityNames);
		List<StoreSupportFacity> saveFacitys = new ArrayList<>();
		for (String facityName : facities) {
			StoreSupportFacity storeSupportFacity = new StoreSupportFacity();
			storeSupportFacity.setName(facityName);
			storeSupportFacity.setCreatetime(new Date());
			storeSupportFacity.setIsUse(0);
			saveFacitys.add(storeSupportFacity);
		}
		facityRepository.save(saveFacitys);
		backLogService.addBackLog(userid, "增加/删除多个门店配套设施>> 修改后的门店配套设施为："+facityNames);
		result.setKey("success");
		result.setMsg("配套设施添加成功");
		result.setValue(facityRepository.findAll());
		return result;
	}

	@Transactional
	@Override
	public Result addStoreSupportServices(String userid,String serviceNames) {
		Result result = new Result();
		List<String> f = Arrays.asList(serviceNames.split("@;-;@"));
		List<String> services = new ArrayList<>(f);
		//删除不存在的门店配套服务
		serviceRepository.dealNotExistsStoreSupportService(services);
		//获取新的门店配套服务
		List<String> allServiceNames = serviceRepository.getAllStoreSupportServiceNames();
		services.removeAll(allServiceNames);
		List<StoreSupportService> saveServices = new ArrayList<>();
		for (String serviceName : services) {
			StoreSupportService service = new StoreSupportService();
			service.setName(serviceName);
			service.setCreatetime(new Date());
			service.setIsUse(0);
			saveServices.add(service);
		}
		serviceRepository.save(saveServices);
		backLogService.addBackLog(userid, "增加/更新多个门店配套服务>> 修改后的门店配套服务为："+serviceNames);
		result.setKey("success");
		result.setMsg("配套服务添加成功");
		result.setValue(serviceRepository.findAll());
		return result;
	}


	@Override
	public List<StoreSupportFacity> getAllFacitys() {
		return facityRepository.findAll();
	}

	@Override
	public List<StoreSupportService> getAllServices() {
		return serviceRepository.findAll();
	}

	@Override
	public Map<String, Object> getAllFacitysAndServices() {
		List<StoreSupportFacity> facities = facityAndServiceNewDao.getAllFacity();
		List<StoreSupportService> services = facityAndServiceNewDao.getAllService();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("facities", facities);
		map.put("services", services);
		return map;
	}

	@Override
	public Result dealStoreSupportFacity(String userid,String facityId) {
		Result result = new Result();
		StoreSupportFacity facity = facityRepository.findOne(facityId);
		Integer count = facityRepository.dealStoreSupportFacity(facityId);
		if(count == 0){
			result.setKey("error_dealFailed");
			result.setMsg("门店配套设施移除失败");
			return result;
		}
		backLogService.addBackLog(userid, "移除门店配套设施>> 门店配套设施id："+facityId + " 门店设施名称："+facity.getName());
		result.setKey("success");
		result.setMsg("门店配套设施移除成功");
		return result;
	}

	@Override
	public Result dealStoreSupportService(String userid,String serviceId) {
		Result result = new Result();
		StoreSupportService service = serviceRepository.findOne(serviceId);
		Integer count = serviceRepository.dealStoreSupportService(serviceId);
		if(count == 0){
			result.setKey("error_dealFailed");
			result.setMsg("门店配套服务移除失败");
			return result;
		}
		backLogService.addBackLog(userid, "移除门店配套服务>> 门店配套服务id："+serviceId+" 门店服务名称："+service.getName());
		result.setKey("success");
		result.setMsg("门店配套服务移除成功");
		return result;
	}

}
